package org.jeecg.modules.extbpm.process.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.extbpm.process.entity.ExtActListener;
import org.jeecg.modules.extbpm.process.service.IExtActListenerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;

 /**
 * @Title: Controller
 * @Description: 流程监听表
 * @author： jeecg-boot
 * @date：   2019-03-15
 * @version： V1.0
 */
@RestController("extActListenerController")
@RequestMapping("/act/process/extActListener")
@Slf4j
public class ExtActListenerController {
	@Autowired
	private IExtActListenerService extActListenerService;
	
	/**
	  * 分页列表查询
	 * @param extActListener
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<IPage<ExtActListener>> queryPageList(ExtActListener extActListener,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<IPage<ExtActListener>> result = new Result<IPage<ExtActListener>>();
		QueryWrapper<ExtActListener> queryWrapper = new QueryWrapper<ExtActListener>(extActListener);
		Page<ExtActListener> page = new Page<ExtActListener>(pageNo,pageSize);
		queryWrapper.eq("del_flag", "0");//删除状态正常
		//排序逻辑 处理
		String column = req.getParameter("column");
		String order = req.getParameter("order");
		if(oConvertUtils.isNotEmpty(column) && oConvertUtils.isNotEmpty(order)) {
			if("asc".equals(order)) {
				queryWrapper.orderByAsc(oConvertUtils.camelToUnderline(column));
			}else {
				queryWrapper.orderByDesc(oConvertUtils.camelToUnderline(column));
			}
		}
		IPage<ExtActListener> pageList = extActListenerService.page(page, queryWrapper);
		//log.debug("查询当前页："+pageList.getCurrent());
		//log.debug("查询当前页数量："+pageList.getSize());
		//log.debug("查询结果数量："+pageList.getRecords().size());
		//log.debug("数据总数："+pageList.getTotal());
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	/**
	  *   添加
	 * @param extActListener
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<ExtActListener> add(@RequestBody ExtActListener extActListener) {
		Result<ExtActListener> result = new Result<ExtActListener>();
		try {
			extActListener.setListenerStatus(0);//禁用
			extActListener.setDelFlag("0");//删除状态正常
			extActListenerService.save(extActListener);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}
	
	/**
	  *  编辑
	 * @param extActListener
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<ExtActListener> edit(@RequestBody ExtActListener extActListener) {
		Result<ExtActListener> result = new Result<ExtActListener>();
		ExtActListener extActListenerEntity = extActListenerService.getById(extActListener.getId());
		if(extActListenerEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = extActListenerService.updateById(extActListener);
			//TODO 返回false说明什么？
			if(ok) {
				result.success("修改成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<ExtActListener> delete(@RequestParam(name="id",required=true) String id) {
		Result<ExtActListener> result = new Result<ExtActListener>();
		ExtActListener extActListener = extActListenerService.getById(id);
		if(extActListener==null) {
			result.error500("未找到对应实体");
		}else {
			extActListener.setDelFlag("1");
			boolean ok = extActListenerService.updateById(extActListener);
//			boolean ok = extActListenerService.removeById(id);
			if(ok) {
				result.success("删除成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<ExtActListener> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<ExtActListener> result = new Result<ExtActListener>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
//			this.extActListenerService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<ExtActListener> queryById(@RequestParam(name="id",required=true) String id) {
		Result<ExtActListener> result = new Result<ExtActListener>();
		ExtActListener extActListener = extActListenerService.getById(id);
		if(extActListener==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(extActListener);
			result.setSuccess(true);
		}
		return result;
	}
	
	/**
	 * 
	 * @Title: 禁用、启用操作   
	 * @Description: TODO(重置状态)   
	 */
	@PutMapping(value = "/changeStatus")
	public Result<ExtActListener> changeStatus(@RequestBody HashMap<String, String> map) {
		String id=map.get("id");
		Result<ExtActListener> result = new Result<ExtActListener>();
		ExtActListener extActListener = extActListenerService.getById(id);
		if(extActListener==null) {
			result.error500("未找到对应实体");
		}else {
			
			if (extActListener.getListenerStatus()==0) {
				extActListener.setListenerStatus(1);
			}else if (extActListener.getListenerStatus()==1) {
				extActListener.setListenerStatus(0);
			}
			extActListenerService.updateById(extActListener);
			result.success("操作成功!");
		}
		return result;
	}

}
